//导入自己的css
require('../css/message.css');


let userId = localStorage.getItem('userId');
// 请求获取个人中心的接口
function userInfo(){
    http.get('/users/accountinfo',{userId},function(res){
        console.log(res.data);
        nicknameInp.value = res.data.nickname;
        genderText.textContent = res.data.gender;
        let addressArr = res.data.address.split(',')
        pro.textContent = addressArr[0];
        city.textContent = addressArr[1];
        textarea.value = res.data.sign;
        bornDate.textContent =  res.data.birthday.slice(0,10);
        // 将用户名保存在本地存储中
        localStorage.setItem('nickname',res.data.nickname)
    })
}


// 页面加载调用
userInfo();




// 点击返回上一页按钮，页面跳转
document.querySelector('header i').addEventListener('click',function(){
    location.href = './mine.html';
})
//昵称获取
let nicknameInp = document.querySelector('#js_input2')
// 性别选择
let genderDom = document.querySelector('#gender');
let genderText = document.querySelector('#gender .weui-cell__ft');

genderDom.addEventListener('click',function(){
    weui.picker([{
        label: '男',
        value: '男'
    }, {
        label: '女',
        value: '女'
    }], {
        onConfirm: function (result) {
            console.log(result[0].value);
            genderText.textContent = result[0].value
        },
        title: '选择性别'
    });
}) 


//出生日期选择
let bornDateDom = document.querySelector('#born');
let bornDate = document.querySelector('#born .weui-cell__ft');
bornDateDom.addEventListener('click',function(){
    weui.datePicker({
        start: 1890,
        end: new Date().getFullYear(),
        onConfirm: function (result) {
            let y = result[0].value;
            let m = result[1].value;
            let d = result[2].value;
            bornDate.textContent = `${y}-${m<10?0:''}${m}-${d<10?0:''}${d}`;
        },
        title: '选择出生日期'
    });
})

// 省份选择
let proDom = document.querySelector('#pro');
let pro = document.querySelector('#pro .weui-cell__ft');
let addressId;
// 点击省份。请求省份的接口
proDom.addEventListener('click',function(){
    http.get('/address/province',{},function(res){
        console.log(res);
        // 获取要传给底部弹窗的数据，并转成底部弹窗需要的类型的数据
        let proArr = res.data.map(function(item){
            return {
                label:item.name,
                value:item.addressId
            }
        })
        // 将后台拿到的省份数据渲染到底部弹窗中
        weui.picker(proArr, {
            onConfirm: function (result) {
                // console.log(result[0].value);
                pro.textContent = result[0].label;
                addressId = result[0].value;
                city.textContent = '请选择'
            },
            title: '选择省份'
        });
    })
    
})

// 城市选择
let cityDom = document.querySelector('#city');
let city = document.querySelector('#city .weui-cell__ft');
// 添加点击事件
cityDom.addEventListener('click',function(){
    if (pro.textContent == '请选择') {
        common.tip('请先选择省份')
    }else{
        console.log(addressId);
        // 请求城市接口

        http.get(`/address/city/${addressId}`,{},function(res){
            console.log(res);
            // 获取要传给底部弹窗的数据，并转成底部弹窗需要的类型的数据
            let proArr = res.data.map(function(item){
                return {
                    label:item.name,
                    value:item.addressId
                }
            })
            // 将后台拿到的省份数据渲染到底部弹窗中
            weui.picker(proArr, {
                onConfirm: function (result) {
                    city.textContent = result[0].label;
                },
                title: '选择城市'
            });
        })
    }
})
// 获取多行文本域dom
let textarea = document.querySelector('.weui-form__control-area textarea')

// 点击保存按钮，提交修改数据，请求修改接口，保存数据

let btn = document.querySelector('button')
btn.addEventListener('click',function(){
    // if(!nicknameInp.value  && !genderText.textContent && !bornDate.textContent == '请选择' && pro.textContent == '请选择' && pro.textContent == '请选择' && !textarea.value){
    //     common.tip(0,'您未修改任何信息哦！');
    //     return;
    // }
    // 调用接口
    let data ={
        'userId':userId,     // 必传项
        'nickname':nicknameInp.value,
        'gender':genderText.textContent,
        'birthday':bornDate.textContent,
        'address':[pro.textContent,city.textContent],
        'sign':textarea.value
    }
    http.post('/users/userEdit',data,function(res){
        console.log(res);
        if(res.status == 0){
            userInfo();
            common.tip(1,'保存成功')
            setTimeout(function(){
                location.href = './mine.html'
            },1500)
        }else{
            common.tip(0,'您未进行任何修改哦')
        }  
    })  
})



 

